<div id="content-header">
    <?php
    $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
        'links' => array('Sản phẩm' => 'index', 'Danh mục'),
        'htmlOptions' => array('id' => 'breadcrumb')
    ));
    ?>
</div>
<?php
$form = $this->beginWidget('CActiveForm', array(
    'enableAjaxValidation' => TRUE,
        ));
?>
<div class="row-fluid">
    <div class="span2">
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon">
                    <i class="icon-refresh"></i>
                </span>
                <h5>Quản lý sản phẩm</h5>
            </div>

            <div class="widget-content nopadding">
                <table class="table table-bordered">
                    <tbody>
                        <tr><td><?php echo Chtml::link('Danh mục sản phẩm', array('productCategory/')) ?></td></tr>
                        <tr><td><?php echo Chtml::link('Thêm mới danh mục', array('productCategory/create')) ?></td></tr>
                        <tr><td><?php echo Chtml::link('Thêm mới sản phẩm', array('product/create')) ?></td></tr>
                        <tr><td><?php echo CHtml::ajaxSubmitButton('Xóa nhiều mẫu tin', array('product/deleteAll'), array('success' => 'reloadGrid', 'beforeSend' => 'function(){
            return confirm("Bạn có chắc chắn muốn xóa những mục được chọn?")
        }'), array('class' => 'btn btn-link')); ?></td></tr>
                        <tr><td>Ban đang ở trang danh sách sản phẩm</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="span10">
        <div class="widget-box">
            <div class="widget-content nopadding">
                <div id="DataTables_Table_0_wrapper" class="dataTables_wrapper" role="grid">


                    <?php
                    $this->widget('bootstrap.widgets.TbGridView', array(
                        'id' => 'table-grid',
                        'dataProvider' => $model->search(),
                        'filter' => $model,
                        'selectableRows' => 2,
                        'columns' => array(
                            array(
                                'header' => '#',
                                'class' => 'ext.CounterColumn',
                                'htmlOptions' => array('class' => 'tdheader')
                            ),
                            array(
                                'id' => 'autoId',
                                'class' => 'zii.widgets.grid.CCheckBoxColumn',
                                'selectableRows' => '50',
                            ),
                            array(
                                'name' => 'image',
                                'value' => '"<img src=\"".Yii::app()->request->baseUrl."/uploads/products/".$data->image."\" width=\"50\" height=\"50\"/>"',
                                'type' => 'raw',
                                'filter' => FALSE,
                                'htmlOptions' => array('class' => 'tdimg')
                            ),
                            array(
                                'name' => 'name',
                                'type' => 'raw',
                                'value' => '$data->name',
                                'filter' => FALSE,
                            ),
                            array(
                                'name' => 'price',
                                'type' => 'raw',
                                'value' => 'number_format($data->price, 0, ",", ".")',
                                'filter' => FALSE,
                            ),
                            array(
                                'name' => 'sale',
                                'type' => 'raw',
                                'value' => 'number_format($data->sale, 0, ",", ".")',
                                'filter' => FALSE,
                            ),
                            array(
                                'name' => 'create_time',
                                'type' => 'raw',
                                'value' => 'date("d-m-Y",$data->create_time)',
                                'filter' => FALSE,
                            ),
                            array(
                                'name' => 'state',
                                'type' => 'raw',
                                'value' => '$data->state',
                                'filter' => FALSE,
                            ),
                            array(
                                'name' => 'product_sold',
                                'type' => 'raw',
                                'value' => '"<div id=".$data->id.">".CHtml::link(($data->product_sold==1)?"Đã bán":"Chưa bán",
                                        CHtml::normalizeUrl("javascript:void(0)"),
                                       
                                        array(
                                            "rel" => $data->id,
                                             "onclick"=>"sold(".$data->id.",".$data->product_sold.")",
                                        )
                                    )."</div>"',
                                'filter' => array(1 => 'Đã bán', 0 => 'Chưa bán'),
                                'htmlOptions' => array('class' => 'tdstatus')
                            ),
                            array(
                                'name' => 'status',
                                'type' => 'raw',
                                'value' => '"<div id=".$data->id.">".CHtml::link(($data->status==1)?"Kích hoạt":"Chưa kích hoạt",
                                        CHtml::normalizeUrl("javascript:void(0)"),
                                       
                                        array(
                                            "rel" => $data->id,
                                             "onclick"=>"status(".$data->id.",".$data->status.")",
                                        )
                                    )."</div>"',
                                'filter' => array(1 => 'Kích hoạt', 0 => 'Chưa kích hoạt'),
                                'htmlOptions' => array('class' => 'tdstatus')
                            ),
                            array(
                                'class' => 'CButtonColumn',
                            )
                        ),
                    ));
                    $cs = Yii::app()->getClientScript();
                    $cs->registerScript('reloadgrid', 'function reloadGrid(data){$.fn.yiiGridView.update("table-grid")}', CClientScript::POS_END);
                    ?>
                </div>
            </div>

        </div>
    </div>
</div>
<?php $this->endWidget(); ?>

<script>
    function status(id, status) {
        $.ajax({
            type: 'POST',
            url: 'product/status',
            data: {
                'id': id,
                'status': status
            },
            success: function(data) {
                $.fn.yiiGridView.update('table-grid');
            }
        });

    }
    function sold(id, sold) {
        $.ajax({
            type: 'POST',
            url: 'product/sold',
            data: {
                'id': id,
                'sold': sold
            },
            success: function(data) {
                $.fn.yiiGridView.update('table-grid');
            }
        });

    }

</script>